package com.example.xd.dialtest.js;

import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.example.xd.dialtest.R;

import java.io.IOException;
import java.util.Properties;

import static android.view.KeyEvent.KEYCODE_BACK;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private String url1 = "https://live.v.wo.cn/platform/ytpxz/index.html?cid=xdx";
    private String url2 = "https://topic.vivo.com.cn/vcard/TP1ay5ecw3vjuo/index.html?carrierId=2&special=1&channel=8";
    private String url3 = "https://mp.weixin.qq.com/s?__biz=MzUzODY5NzQxOQ==&mid=2247491264&idx=1&sn=c1f643622f5ced45627eedef78a396cd&chksm=fad29564cda51c72923f6eae892e2b2d2f6fe0896d16b81830510b78859240de971293514acd&mpshare=1&scene=1&srcid=&sharer_sharetime=1575961484058&sharer_shareid=9abcf6fde587555ad76bd302fc3e347a#rd";
    private String url4 = "file:///android_asset/index.html";
    private WebView mWeb_view;
    private InJavaScriptLocalObj mInJavaScriptLocalObj;
    Handler mHandler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mWeb_view = findViewById(R.id.web_view);
        findViewById(R.id.btn).setOnClickListener(this);

        initWebView();
    }

    private void initWebView() {
        mWeb_view.getSettings().setJavaScriptEnabled(true);
        mInJavaScriptLocalObj = new InJavaScriptLocalObj();
        mWeb_view.addJavascriptInterface(mInJavaScriptLocalObj, "java_obj");
        mWeb_view.requestFocus();
        mWeb_view.canGoBack();
        mWeb_view.loadUrl(url4);
        mWeb_view.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }


            @Override
            public void onPageFinished(WebView view, String url) {
                view.loadUrl("javascript:window.java_obj.getImgSrcById(document.getElementById('img_id').src);");
                view.loadUrl("javascript:window.java_obj.getHContentById(document.getElementById('h1_id').innerHTML);");
                view.loadUrl("javascript:window.java_obj.getAHerfById(document.getElementById('a_id').href);");
                view.loadUrl("javascript:window.java_obj.getLiById(document.getElementById('li_id1').innerHTML);");
                view.loadUrl("javascript:window.java_obj.getLiById(document.getElementById('li_id2').innerHTML);");
                super.onPageFinished(view, url);
            }

        });
        mWeb_view.setWebChromeClient(new WebChromeClient() {
            @Override
            public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
                return super.onJsAlert(view, url, message, result);
            }
        });
    }

    private void test() throws IOException {
        Properties properties = new Properties();
        properties.load(getAssets().open("pro.properties"));
        String name_id = properties.getProperty("name_id");
        String market_id = properties.getProperty("market_id");
        Log.i("xd---", name_id + "--" + market_id);
    }

    @Override
    public void onClick(View v) {
//        getInputContent();
//        setInputContent();
//        test_function();


//        getRadioChecked();
        getCheckBoxchecked();
    }


    private void getCheckBoxchecked() {
        mWeb_view.loadUrl("javascript:getCheckBoxChecked('vehicle');");
    }

    private void test_function() {
        mWeb_view.loadUrl("javascript:test(3,2)");
    }

    private void getRadioChecked() {
        mWeb_view.loadUrl("javascript:getRadioChecked('sex')");
    }

    private void getInputContent() {
        mWeb_view.loadUrl("javascript:window.java_obj.getInputContentById(document.getElementById('input_id1').value);");
        mWeb_view.loadUrl("javascript:window.java_obj.getInputContentById(document.getElementById('input_id2').value);");
    }

    private void setInputContent() {
        mWeb_view.loadUrl("javascript:setInputContent('input_id1','1')");
        mWeb_view.loadUrl("javascript:setInputContent('input_id2','2')");
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KEYCODE_BACK) && mWeb_view.canGoBack()) {
            mWeb_view.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
